//
//  XYHomeTableViewCell.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/17.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYHomeTableViewCell: UITableViewCell {

    
    var homeCellModel:XYHomeCellF?{
        didSet{
            setCellData()
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        //cell选中时的颜色
        self.selectionStyle = UITableViewCellSelectionStyle.Gray
        
        contentView.backgroundColor = ColorDefine(r: 240, g: 240, b: 240)
        //1.添加cell的原创微博
        contentView.addSubview(originalView)
        
        //2.添加cell的转发微博内容
        contentView.addSubview(retweetedView)
        
        //3.添加评论工具
        contentView.addSubview(toolBar)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //得到frame模型之后的控件赋值
    private func setCellData(){
        //取出frame模型
        let statusF = self.homeCellModel!
        //取出数据模型
        let status = statusF.status!
        
        
        //给整个原创微博设置frame
        originalView.frame = statusF.originalViewF!
        
        //给头像设置frame和数据
        userProfile_image.frame = statusF.userProfile_imageF!
        userProfile_image.sd_setImageWithURL(NSURL(string: status.user!.profile_image_url!), placeholderImage: UIImage(named: "avatar_default_big"))
        
        //给姓名设置frame和数据
        userName.frame = statusF.userNameF!
        userName.text = status.user!.name
        
        //给会员图标设置frame和数据
        if status.user!.isVip{
            vipImage.hidden = false
            vipImage.frame = statusF.vipImageF!
            vipImage.image = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
            
            userName.textColor = UIColor.orangeColor()
        }else{
            vipImage.hidden = true
            userName.textColor = UIColor.blackColor()
        }
        
        //给创建时间设置frame和数据
        let createdText = status.created_atStr
        createdTime.frame = statusF.createdTimeF!
        //把时间的font传进去.计算大小 .在赋值给label(动态获取时间label的宽度)
        createdTime.width = createdText.size(createdTime.font).width
        createdTime.text = createdText
        
        //给微博来源设置frame和数据
        source.frame = statusF.sourceF!
        source.text = status.source
        
        //给原创微博设置frame和数据
        originateText.frame = statusF.originateTextF!
        originateText.text = status.text
        
        //给原创微博图片设置frame和数据
        if let pic = status.pic_urls where pic.count != 0{
            originateImage.hidden = false
            originateImage.frame = statusF.originateImageF!
            originateImage.pic_urls = pic
        }else{
            originateImage.hidden = true
        }
        
        //给转发微博设置数据
        if let retweeted = status.retweeted_status {
            retweetedView.hidden = false
            retweetedView.frame = statusF.retweetedViewF!
            //给转发内容赋值
            retweetedText.frame = statusF.retweetedTextF!
            retweetedText.text = retweeted.text
            if let retweetedPic = retweeted.pic_urls where retweetedPic.count != 0{
                retweetedImage.hidden = false
                retweetedImage.frame = statusF.retweetedImageF!
                retweetedImage.pic_urls = retweetedPic
            }else{
                retweetedImage.hidden = true
            }
        }else{
            retweetedView.hidden = true
        }
        
        
        //给底部toolbar设置frame
        toolBar.frame = statusF.toolBarF!
        toolBar.status = status
    }
    
    
    //整个原创微博的view
    private lazy var originalView:UIView = {
        
        let view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        
        //添加作者头像
        view.addSubview(self.userProfile_image)
        //添加作者姓名
        view.addSubview(self.userName)
        //添加创建时间
        view.addSubview(self.createdTime)
        //添加创建来源
        view.addSubview(self.source)
        //添加原创微博内容
        view.addSubview(self.originateText)
        //添加会员图标
        view.addSubview(self.vipImage)
        //添加原创配图
        view.addSubview(self.originateImage)
        
        return view
    }()
    
    //用户微博头像
    private lazy var userProfile_image:UIImageView = {
        let view = UIImageView()
        view.backgroundColor = UIColor.blackColor()
        
        view.layer.borderColor = UIColor.grayColor().CGColor
        view.layer.borderWidth = 1
        
        return view
    }()
    
    //用户名称
    private lazy var userName:UILabel = {
        
        let nameLabel = UILabel()
        
        nameLabel.font = UIFont.systemFontOfSize(CELL_NAME_FONT)
        
        return nameLabel
        
    }()
    
    //微博创建的时间
    private lazy var createdTime:UILabel = {
        
        let timeLabel = UILabel()
        
        timeLabel.font = UIFont.systemFontOfSize(CELL_TIME_SOURCE_FONT)
        
        timeLabel.textColor = UIColor.grayColor()
        
        return timeLabel
        
        }()
    
    //微博的来源
    private lazy var source:UILabel = {
        
        let sourceLabel = UILabel()
        
        sourceLabel.font = UIFont.systemFontOfSize(CELL_TIME_SOURCE_FONT)
        
        sourceLabel.textColor = UIColor.grayColor()
        
        return sourceLabel
        
        }()
    
    //原创微博的内容
    private lazy var originateText:UILabel = {
        
        let textLabel = UILabel()
        
        textLabel.numberOfLines = 0
        textLabel.font = UIFont.systemFontOfSize(CELL_TEXT_FONT)
        
        return textLabel
        
        }()
    
    //vip
    private lazy var vipImage:UIImageView = UIImageView()
    
    //原创微博的配图
    private lazy var originateImage:XYOriginateView = XYOriginateView()
    
    //转发微博的整个view
    private lazy var retweetedView:UIView = {
        
        let view = UIView()
        view.backgroundColor = ColorDefine(r: 244, g: 244, b: 244)
        //添加转发微博内容
        view.addSubview(self.retweetedText)
        //添加转发微博的配图
        view.addSubview(self.retweetedImage)
        
        return view
        }()
    
    //转发微博的内容
    private lazy var retweetedText:UILabel = {
        
        let textLabel = UILabel()
        
        textLabel.numberOfLines = 0
        textLabel.font = UIFont.systemFontOfSize(CELL_TEXT_FONT)
        
        return textLabel
        
        }()

    //转发微博的配图
    private lazy var retweetedImage:XYOriginateView = XYOriginateView()

    
    //底部转发评论点赞按钮
    private lazy var toolBar:XYToolBar = XYToolBar()
}









